// ===================================
// <copyright>
// This file is part of LABSMFController.
//
//    LABSMFController is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    LABSMFController is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with LABSMFController.  If not, see <http://www.gnu.org/licenses/>.
//    Copyright 2011 David Bakker
//
//</copyright>
// <author>David Bakker</author>
// <email>mailto:DavidFBakker@gmail.com</email>
// <created>Saturday, May 07, 2011</created>
// <lastedit>Saturday, May 07, 2011</lastedit>
// ===================================

using System.Ext;
using System.Threading;
using GHIElectronics.NETMF.FEZ;

namespace LABSMFController.LABS.Services
{
    internal class RelayService : BeerService
    {
        private static readonly FEZ_Components.Relay16 MyRelay16 = new FEZ_Components.Relay16();

        private static bool _isRunning;
        private static object _locker;

        public override void Configure()
        {
            Console.Write("RelayService configure");
            _isRunning = false;
            _locker = new object();
        }

        public override void Start()
        {
            Console.Write("RelayService start");
            _isRunning = false;
        }

        public override void Stop()
        {
            Console.Write("RelayService stop");
        }

        public void On(FEZ_Components.Relay16.RelayChannel relayChannell)
        {
            while (_isRunning) Thread.Sleep(100);
            lock (_locker)
            {
                _isRunning = true;
                
                    MyRelay16.TurnOn(relayChannell);
                  
                _isRunning = false;
            }
        }

        public void Off(FEZ_Components.Relay16.RelayChannel relayChannell)
        {
            while (_isRunning) Thread.Sleep(100);
            lock (_locker)
            {
                _isRunning = true;

                
                    MyRelay16.ShutOff(relayChannell);
                   

                _isRunning = false;
            }
        }

        public void AllOff()
        {
            while (_isRunning) Thread.Sleep(100);
            lock (_locker)
            {
                _isRunning = true;
                MyRelay16.ShutAllOff();
              
                _isRunning = false;
            }
        }
    }
}